//
//  MoreView.m
//  PLRLiveShow
//
//  Created by zhaoxiaoyan on 2017/8/16.
//  Copyright © 2017年 xiaoyanzhao. All rights reserved.
//

#import "MoreView.h"
#import "UIScreen+ScreenSize.h"
#import "MoreButtonCellTableViewCell.h"
#import "CustomTableViewCell.h"
#import "CellDataModel.h"
#import "SectionDataModel.h"

@interface MoreView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) NSMutableArray *mutableArray;

@end

@implementation MoreView

- (instancetype)initWithFrame:(CGRect)frame withArray:(NSMutableArray *)array {
    self = [super initWithFrame:frame];
    if (self) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen screenwidth], [UIScreen screenHeight] - 49) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithRed:245.0/255 green:245.0/255 blue:245.0/255 alpha:1];
        [self addSubview:_tableView];
        
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = UITableViewAutomaticDimension;

        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
        [_tableView registerClass:[MoreButtonCellTableViewCell class] forCellReuseIdentifier:@"moreCellID"];
        [_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"customID"];
        _mutableArray = array;
        
    }
    return self;
}

- (UIView *)headerView {
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen screenwidth], 120)];
    _headerView.backgroundColor = [UIColor greenColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 50, 50, 50)];
    imageView.backgroundColor = [UIColor redColor];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 65, 70, 20)];
    nameLabel.backgroundColor = [UIColor lightGrayColor];
    [_headerView addSubview:nameLabel];
    [_headerView addSubview:imageView];
    
    return _headerView;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    }
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 12;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 6) {
        return 12;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40.f;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1 || section == 2) {
        return 1;
    } else if (section == 3) {
        return 3;
    } else if (section == 4 || section == 5 || section == 6) {
        return 2;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreCellID"];
        return cell;
    } else {
        CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customID"];
        SectionDataModel *sectionDataModel = _mutableArray[indexPath.section - 1];
        CellDataModel *cellDataModel = sectionDataModel.mutableArray[indexPath.row];
        cell.dataModel = cellDataModel;
        return cell;
    }
    return nil;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 7;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, -sectionHeaderHeight, 0);
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight && scrollView.contentOffset.y <= scrollView.contentSize.height - scrollView.frame.size.height - sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionHeaderHeight, 0);
    } else if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height - sectionHeaderHeight && scrollView.contentOffset.y <= scrollView.contentSize.height - _tableView.frame.size.height) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, -(scrollView.contentSize.height - scrollView.frame.size.height - sectionHeaderHeight), 0);
    }
}

@end
